package com.abocode.aichat.server.stanza;

import com.abocode.aichat.server.entity.Entity;
import com.abocode.aichat.server.entity.EntityImpl;
import com.abocode.aichat.server.entity.Entity;
import com.abocode.aichat.server.entity.EntityImpl;

import java.util.Date;
import java.util.Map;

/**
 * Created by franky-6.
 */
public class StanzaImpl implements Stanza {

    private String id;
    private Map body;
    private String namespace;
    private String module;
    private Long time=new Date().getTime() / 1000;

    public Long getTime() {
        return time;
    }

    public void setTime(Long time) {
        this.time = time;
    }

    public StanzaImpl() {
         
    }

    public String getId() {
        return id;
    }

    public Map getBody() {

        return body;
    }

    public String getNamespace() {
        return namespace;
    }

    public String getModule() {
        return module;
    }

    public StanzaImpl(String id, Map body, String namespace, String module) {
        this.id = id;
        this.body = body;
        this.namespace = namespace;
        this.module = module;
    }

    public String namespace() {
        return namespace;
    }

    public void setNamespace(String namespace) {
        this.namespace = namespace;
    }

    public String module() {
        return module;
    }

    public void setModule(String module) {
        this.module = module;
    }

    public String id() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Map body() {
        return body;
    }

    public void setBody(Map body) {
        this.body = body;
    }

    @Override
    public String name() {
        return namespace();
    }

    @Override
    public void validate() {
        if (id() == null || module() == null || namespace() == null) {
            throw new NullPointerException("id | module | namespace must not be null");
        }
    }

    public Entity findFrom() {
        return findEntity("from");
    }

    public Entity findTo() {
       return findEntity("to");
    }

    private Entity findEntity(String key){
        try {
            Object obj=body().get(key);
            if(obj==null){
                return null;
            }
            Entity entity = EntityImpl.parse(String.valueOf(obj));
            return entity;
        } catch (Exception ex) {
            return null;
        }
    }



    @Override
    public String token() {
        return null;
    }
}

